package structs;

import interfaces.ICard;
import interfaces.Strategy;

public class SmarterTeamStrategy implements Strategy {

	private Game game;
	private DeckInformation deckInfo;
	private BooleanStrategy booleanStrategy;
	private ICard throwCard;
	private CardProbability cardProb;
	private boolean beginning = true;
	private CardStrategy cardStrategy;

	public SmarterTeamStrategy(Game game)
	{
		this.game = game;
	}


	public ICard nextMove() {
		if(beginning)
		{
			initializeStrategy();
			beginning = false;
		}
		
		playGame();
		return throwCard;
	}
	
	public void initializeStrategy() {
		deckInfo = new DeckInformation(this.game);
		deckInfo.initializeDeckInfo();
		cardProb = new CardProbability(this.game, this.deckInfo);
		cardProb.initialProbability();
		cardStrategy = new CardStrategy(this.game, this.cardProb);
		booleanStrategy = new BooleanStrategy(this.game, this.cardStrategy);
		
	}

	public void playGame()
	{
		deckInfo.updateDeckInfo();
		cardProb.updateProbability();
		cardStrategy.updateCardStrategy();
		booleanStrategy.updateBooleanStrategy();
		

		if(this.game.getRoundNumber()<=9)
		{
			switch (this.game.getAIPosition()) 
			{
			case 1: turn1(); break;
			case 2: turn2(); break;
			case 3: turn3(); break;
			case 4: turn4(); break;
			default: break;
			}
		}
		
		else if(this.game.getRoundNumber() == 10){
			this.throwCard = this.game.getAIPlayer().getCurrentHand().get(0);
		}
		
		else 
		{
			//Ricardo algoritmo de ultimas 3 cartas.
			this.lastTurns();
		}
	}

	public void turn1()
	{
		throwCard = this.cardStrategy.lowestCardWithHighProbOfLossing();
	}

	public void turn2()
	{
		if(booleanStrategy.doWeNeedToWin())
		{
			throwCard = this.cardStrategy.highestSuitLife();
		}
		
		else
		{
			throwCard = this.cardStrategy.lowestSuitNoneLife();
		}
	}

	public void turn3()
	{
		if(booleanStrategy.doWeNeedToWin())
		{
			
			throwCard = this.cardStrategy.highestSuitLife();
			if(throwCard.getPoints()>=10)
			{
				throwCard = this.cardStrategy.lowestLife();
			}
		}
		
		else
		{
			throwCard = this.cardStrategy.lowestSuitNoneLife();
		}
	}

	public void turn4()
	{
		// La logica esta ya termina faltan las probabilidades
		if(booleanStrategy.areWeWinning())
		{
			throwCard = cardStrategy.highestNoneLife();
		}

		else if(booleanStrategy.doWeNeedToWin())
		{
			throwCard = cardStrategy.highestSuitLife();
		}
		//ANADIR METODO CAN WE WIN
		
		else if (!cardStrategy.cardThatIsWinning().getSuit().equalsIgnoreCase(this.game.getDeck().getLife().getSuit()) 
				&& ((this.cardStrategy.hand1.getPoints()>=10 
						&& this.cardStrategy.cardThatIsWinning().getSuit().equalsIgnoreCase(this.cardStrategy.hand1.getSuit())) 
						|| (this.game.getAIPlayer().getCurrentHand().size()>1 
								&& this.cardStrategy.hand2.getPoints()>=10
								&& this.cardStrategy.cardThatIsWinning().getSuit().equalsIgnoreCase(this.cardStrategy.hand2.getSuit())) 
								|| (this.game.getAIPlayer().getCurrentHand().size() >2 && this.cardStrategy.hand3.getPoints()>=10
										&& this.cardStrategy.cardThatIsWinning().getSuit().equalsIgnoreCase(this.cardStrategy.hand3.getSuit()))))
		{
			throwCard = this.cardStrategy.highestWinningSuit();
		}
		else if (cardStrategy.currentCardsPlayedPoints() >=8)
		{
			throwCard = this.cardStrategy.lowestWinningSuit();
		}
		
		else 
		{
			throwCard = this.cardStrategy.lowestCard();
		}
	}
	
	private void lastTurns(){
		if(this.game.getRoundNumber() == 8){
			if(this.game.getAIPosition() == 1 || this.game.getAIPosition() == 2){
				//Tirar vida bajita
				//Tirar carta q no tenga briscas alta
				this.throwCard = this.cardStrategy.lastFirstTurn();
			}
			else if(this.game.getAIPosition() == 3 || this.game.getAIPosition() == 4){
				//Si tiraton briscas, tratar de matar
				//Si puedo subir, tirarla (y q no halla muchos puntos)
				//Tirar basura
				this.throwCard = this.cardStrategy.lastTurn(this.booleanStrategy);
			}
		}
		else if(this.game.getRoundNumber() == 9){
			if(this.game.getAIPosition() == 1 || this.game.getAIPosition() == 2){
				this.throwCard = this.cardStrategy.lastFirstTurn();
			}
			else if(this.game.getAIPosition() == 3  || this.game.getAIPosition() == 4){
				this.throwCard = this.cardStrategy.lastTurn(this.booleanStrategy);
			}
		}
		else if(this.game.getRoundNumber() == 10){
			//Throw remaining hand
			this.throwCard = this.game.getAIPlayer().getCurrentHand().get(0);
		}
	}
}